/*=============================================================================
// File Name: 
// Author:  Wood.Wang
// Date:
// Message:
// Copyright 2012,All right is reserved;
=============================================================================*/
/* Includes -----------------------------------------------------------------*/
#include "drv_rpr0521rs.h"

#define     I2C_NUM     0
uint8_t     _als_data0_gain,_als_data1_gain;
uint16_t    _als_measure_time;

uint8_t RPR0521RS_Init(void)
{
    uint8_t     temp,index;

    uint8_t     als_gain_table[] = {1, 2, 64, 128};
    uint16_t    als_meas_time_table[] = {0,0,0,0,0,100,100,100,100,100,400,400,50,0,0,0};

    temp = I2C_ReadByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_MANUFACT_ID);
    if(temp != RPR0521RS_MANUFACT_ID_VAL)
    {
        return 1;
    }
    temp = I2C_ReadByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_SYSTEM_CONTROL);
    if((temp&0x3f) != RPR0521RS_PART_ID_VAL)
    {
        return 1;
    }

    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_ALS_PS_CONTROL, RPR0521RS_ALS_PS_CONTROL_VAL);

    temp = I2C_ReadByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_PS_CONTROL);
    temp |= RPR0521RS_PS_CONTROL_VAL;
    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_PS_CONTROL, temp);

    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_MODE_CONTROL, RPR0521RS_MODE_CONTROL_VAL);

    temp = RPR0521RS_ALS_PS_CONTROL_VAL;
    index = (temp >> 4) & 0x03;
    _als_data0_gain = als_gain_table[index];
    index = (temp >> 2) & 0x03;
    _als_data1_gain = als_gain_table[index];

    index = RPR0521RS_MODE_CONTROL_VAL & 0x0F;
    _als_measure_time = als_meas_time_table[index];

    return 0;
}



void RPR0521RS_get_rawdata(uint8_t *buff)
{
    I2C_PageRead(I2C_NUM, RPR0521RS_DEVICE_ADDRESS,
                RPR0521RS_PS_DATA_LSB,
                buff, 6);
}



float RPR0521RS_convert_lx(unsigned short *f)
{
    float lx;
    float d0, d1, d1_d0;

    if (_als_data0_gain == 0) {
        return (-1);
    }

    if (_als_data1_gain == 0) {
        return (-1);
    }

    if (_als_measure_time == 0) 
    {
        return (-1);
    } else if (_als_measure_time == 50) 
    {
        if ((f[0] & 0x8000) == 0x8000) 
        {
            f[0] = 0x7FFF;
        }
        if ((f[1] & 0x8000) == 0x8000) 
        {
            f[1] = 0x7FFF;
        }
    }

    d0 = (float)f[0] * (100 / _als_measure_time) / _als_data0_gain;
    d1 = (float)f[1] * (100 / _als_measure_time) / _als_data1_gain;

    if (d0 == 0) {
        lx = 0;
        return (lx);
    }

    d1_d0 = d1 / d0;

    if (d1_d0 < 0.595) {
        lx = (1.682 * d0 - 1.877 * d1);
    } else if (d1_d0 < 1.015) {
        lx = (0.644 * d0 - 0.132 * d1);
    } else if (d1_d0 < 1.352) {
        lx = (0.756 * d0 - 0.243 * d1);
    } else if (d1_d0 < 3.053) {
        lx = (0.766 * d0 - 0.25 * d1);
    } else {
        lx = 0;
    }

    return (lx);
}


void RPR0521RS_get_psalsval(unsigned int *ps, float *als)
{
    unsigned char val[6];
    unsigned short rawps;
    unsigned short rawals[2];

    RPR0521RS_get_rawdata(val);

    rawps     = ((unsigned short)val[1] << 8) | val[0];
    rawals[0] = ((unsigned short)val[3] << 8) | val[2];
    rawals[1] = ((unsigned short)val[5] << 8) | val[4];

    *ps  = rawps;
    *als = RPR0521RS_convert_lx(rawals);

}


uint8_t RPR0521RS_read_int(void)
{
    return I2C_ReadByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_INTERRUPT_CONTROL);
}

void RPR0521RS_write_int(uint8_t intvalue)
{
    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_INTERRUPT_CONTROL,intvalue);
}


void RPR0521RS_Set_PS_threshold(uint16_t ps_high,uint16_t ps_low)
{
    uint8_t buff[4];
    buff[0] = ps_high&0xff;
    buff[1] = (ps_high>>8)&0x0f;
    buff[2] = ps_low&0xff;
    buff[3] = (ps_low>>8)&0x0f;	
    I2C_PageWrite(I2C_NUM, RPR0521RS_DEVICE_ADDRESS,
                RPR0521RS_PS_TH_LSB,
                buff, 4);
}

void RPR0521RS_Set_ALS_threshold(uint16_t als_high,uint16_t als_low)
{
    uint8_t buff[4];
    buff[0] = als_high&0xff;
    buff[1] = (als_high>>8)&0xff;
    buff[2] = als_low&0xff;
    buff[3] = (als_low>>8)&0xff;
    I2C_PageWrite(I2C_NUM, RPR0521RS_DEVICE_ADDRESS,
                RPR0521RS_PS_TH_LSB,
                buff, 4);
}

void RPR0521RS_power_down(void)
{
    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_SYSTEM_CONTROL,0x80);
    I2C_WriteByte(I2C_NUM, RPR0521RS_DEVICE_ADDRESS, RPR0521RS_MODE_CONTROL,0);
}


#if UART_SHELL == NR_MICRO_SHELL
#include "nr_micro_shell.h"

void rpr0521rs_read(char argc, char *argv)
{
    unsigned int    ps;
    float           als;
    
    printf("rpr0521rs:%02X\r\n",RPR0521RS_Init());
    RPR0521RS_get_psalsval(&ps, &als);
    printf("ps=%u,\t als=%f.\r\n",ps,als);
}

NR_SHELL_CMD_EXPORT(rpr0521,   rpr0521rs_read,     "get light senser rpr0521rs data");

#endif

